package com.yonyou.cyxdms.sysAuth.service.controller.manage.vehicleProduct;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yonyou.cyx.framework.controller.base.BaseController;
import com.yonyou.cyxdms.sysAuth.service.dto.DealerBrandDTO;
import com.yonyou.cyxdms.sysAuth.service.dto.vehicleBrand.DealerBrandListDTO;
import com.yonyou.cyxdms.sysAuth.service.service.vehicleProduct.DealerbrandService;
import com.yonyou.cyxdms.sysAuth.service.vo.vehicleProduct.DealerBrandVO;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.UriComponentsBuilder;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author     ：quankk
 * @date       ：2019/7/17 15:29
 * @description：
 */
@Api(value = "DealerBrandController", tags = {"DealerBrandController"})
@RestController
@RequestMapping("/vehicleProduct/dealerBrand")
public class DealerBrandController extends BaseController {

    // 定义日志接口
    private static final Logger logger = LoggerFactory.getLogger(DealerBrandController.class);

    @Autowired
    private DealerbrandService dealerbrandService;

    /**
     * @author     ：quankk
     * @date       ：2019/7/17 15:30
     * @description：根据查询条件返回对应的品牌经销商关联信息
     */
    @RequestMapping(method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "根据查询条件返回对应的品牌经销商关联信息")
    @ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string", paramType = "header")})
    public IPage<DealerBrandVO> querydealerBrand(@ApiParam(name = "pageNum", value = "当前页") @RequestParam("pageNum") Long current,
                                                 @ApiParam(name = "limit", value = "分页大小") @RequestParam("limit") Long size,
                                                 @RequestParam Map<String, String> param) {
        Page<DealerBrandVO> page = new Page(current, size);
        return dealerbrandService.querydealerBrand(page, param);
    }

    /**
     * @author     ：quankk
     * @date       ：2019/7/17 17:39
     * @description：新增关联信息
     */
    @RequestMapping(value="/save",method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "新增关联信息")
    @ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string", paramType = "header")})
    public ResponseEntity<DealerBrandDTO> addRelation(@RequestBody @Valid DealerBrandDTO dbDto, UriComponentsBuilder uricb) {
        Integer id = dealerbrandService.addRelation(dbDto);
        MultiValueMap<String, String> headers = new HttpHeaders();
        headers.set("Location", uricb.path("/vehicleProduct/dealerBrand/save/{id}").buildAndExpand(id).toUriString());
        return new ResponseEntity<>(dbDto, headers, HttpStatus.CREATED);
    }

    /**
     * @author     ：quankk
     * @date       ：2019/7/17 18:24
     * @description：批量关联
     */
    @RequestMapping(value="/relate",method = RequestMethod.PUT, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "批量关联")
    @ResponseStatus(HttpStatus.CREATED)
    @ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string", paramType = "header")})
    public void relateDealerBrand(@RequestBody DealerBrandListDTO dealerBrandDto) {
        dealerbrandService.relateDealerBrand(dealerBrandDto);
    }

    /**
     * 删除关联信息
     * @author ZhengHe
     * @date 2016年7月5日
     * @param id
     */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    @ResponseStatus(HttpStatus.CREATED)
    @ApiOperation(value = " 删除关联信息")
    @ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string", paramType = "header")})
    public void deleteRelation(@PathVariable("id") Integer id) {
        dealerbrandService.deleteRelate(id);
    }

    /**
    * 设置主数据
    * @author sunguowei
    * @date 2018年4月13日
    * @param id
    */
    @RequestMapping(value = "/primaryBrand/{id}", method = RequestMethod.PUT)
    @ResponseStatus(HttpStatus.CREATED)
    @ApiOperation(value = " 设置主数据")
    @ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string", paramType = "header")})
    public void primaryDealerBrand(@PathVariable("id") Integer id) {
        dealerbrandService.primaryDealerBrand(id);
    }
    
    /**
     * @author     ：quankk
     * @date       ：2019/7/19 17:23
     * @description： 新增页面品牌下拉框的方法
     */
    @RequestMapping(value = "/groupbrand", method = RequestMethod.GET)
    @ApiOperation(value = " 新增页面品牌下拉框的方法")
    @ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string", paramType = "header")})
    public List<Map> brandsSelect2(@RequestParam Map<String,String> queryParam) {
        return dealerbrandService.queryBrand2(queryParam);
    }

    /**
     *  获取品牌列表
     *
     * @param ownerCode
     * @return java.util.List<java.lang.Long>
     * @author dengqiang
     * @since 2019/8/16
     */
    @GetMapping(value = "/queryBrandIdListByOwnerCode")
    public List<Long> queryBrandIdListByOwnerCode(@RequestParam(name = "ownerCode") String ownerCode) {
        return dealerbrandService.queryBrandIdListByOwnerCode(ownerCode);
    }

    /**
     * 获取品牌ID列表
     *
     * @param
     * @return java.util.List<java.lang.Long>
     * @author dengqiang
     * @since 2019/8/19
     */
    @GetMapping(value = "/queryBrandIdList")
    public List<Long> queryBrandIdList() {
        return dealerbrandService.queryBrandIdList();
    }

    /**
     * 获取车系ID列表
     *
     * @author 李贺
     * @since 2019/12/11
     */
    @GetMapping(value = "/querySeriesIdList")
    public List<Long> querySeriesIdList() {
        return dealerbrandService.querySeriesIdList();
    }
    
}
